<?php

//$info = array('1','2','3');
//
//$s = gzcompress(serialize($info),9);
//
//
//$r = unserialize(gzuncompress($s));
//
//print_r($r);

	include_once 'common/common.function.php';
	include_once 'common/database.class.php';
	$db = new DataBase();
	
	/** 数据库版本 **/
	$all_table = $table_info = array();
	$rows = $db->get_all("show tables");
	foreach($rows as $tmp){
		foreach($tmp as $table){
			$all_table[] = $table;
		}
	}	
	foreach($all_table as $table){		
		$craete_table = $db->get_one(" show create table ".$table);
		$table_info[$table]['create'] = $craete_table['Create Table'];
		
		$fields = $db->get_one(" show fields from ".$table);
		$table_info[$table]['fields'] = $fields;
		
		$indexs = $db->get_all(" show index from ".$table);
		$new_index = array();
		foreach ($indexs as $k=>$v){
			$KeyName=$v['Key_name'];
			if(isset($new_index[$KeyName])){
				$new_index[$KeyName]['Column'].=','.$v['Column_name'];
				$new_index[$KeyName]['IsAutoInc']=0;//复合索引不可能有自增字段
			}else{
				$new_index[$KeyName]['Column']=$v['Column_name'];
				$new_index[$KeyName]['NoUnique']=intval($v['Non_unique']);
				$new_index[$KeyName]['IsAutoInc']=$OriFields[$v['Column_name']]['Extra']=='auto_increment'?1:0;
			}
		}
		$table_info[$table]['index'] = $new_index;
	}
	$verno = 1;	
	$sql_file = '__DBSQL_'.$verno.'.php';
	$sql_dir = dirname($sql_file);
	if(!is_dir($sql_dir)){
		create_dir($sql_dir);
	}
	$ser = serialize($table_info);
	$s = gzcompress($ser);
	file_put_contents($sql_file, $s);
	
	/*	
	$filepath = 'sql';
	$filesize = strlen($s);
	$filemd5 = md5($s);
	$sql = "insert into yabb_sys_file 
	(`Id`,`VerNo`,`FileName`,`FilePath`,`FileSize`,`Md5`) values 
	(NULL,{$verno},'{$sql_base_file}','{$filepath}',{$filesize},'{$filemd5}') 
	";		
	$db->execute($sql);	
	*/
	
	
	/** 数据库版本 **/